﻿using Consul;
using iS3.Server.Core;
using iS3.Server.MetaConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace iS3.Server.ExtensionTemplate
{
    class Program
    {
        static void Main(string[] args)
        {
            DMTManager.getInstance().Init();   //   初始化引擎

            SetConsoleCtrlHandler(cancelHandler, true);
            StartWebApi.Start();


            Console.ReadLine();

        }

        //关闭事件
        public delegate bool ControlCtrlDelegate(int CtrlType);
        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerRoutine, bool Add);
        private static ControlCtrlDelegate cancelHandler = new ControlCtrlDelegate(HandlerRoutine);

        public static bool HandlerRoutine(int CtrlType)
        {
            using (var consulClient = new ConsulClient(ConsulManager.consulConfig))
            {
                Console.WriteLine("应用退出，开始从consul注销");
                consulClient.Agent.ServiceDeregister(ConsulManager.serviceId).Wait();
            }
            Console.ReadLine();
            return false;
        }
    }
}
